package com.fick.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts.actions.DispatchAction;

import java.util.List;

import com.fick.hibernate.dao.*;
import com.fick.hibernate.model.*;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ManageJobDetailAction extends DispatchAction {

	private static final Log log = LogFactory.getLog(ManageJobDetailAction.class);

	public ActionForward manageJD(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String jid = request.getParameter("jid");
		String jobName = request.getParameter("jobName");
		String addDate = request.getParameter("date");
		String content = request.getParameter("content");
		String jcid = request.getParameter("jobclass");
		String num = request.getParameter("num");
		String datelimit = request.getParameter("datelimit");
		String money = request.getParameter("money");
		String workaddress = request.getParameter("workaddress");
		Job job = new Job();
		JobDAO jobDAO = new JobDAO();
		JobclassDAO jcDAO = new JobclassDAO();
		Jobclass jc = new Jobclass();
		
		try{
	   
			SimpleDateFormat   sdf   =   new   SimpleDateFormat("yyyy-MM-dd");   
			Date date = new Date();
			if(addDate != null && !addDate.trim().equals(""))
			{
				date = sdf.parse(addDate);
			}  
			
			Date ldate = new Date();
			if(datelimit != null && !datelimit.trim().equals(""))
			{
				ldate = sdf.parse(datelimit);
			}
			
			
			    
			if(jid == null || jid.trim().equals(""))
			{
				jc = jcDAO.findById(Integer.valueOf(jcid));
				job.setJobclass(jc);
				job.setJdes(content);
				job.setJname(jobName);
				job.setAddtime(date);
				job.setNum(Integer.valueOf(num));
				job.setDatelimit(ldate);
				if(money != null &&!money.trim().equals(""))
				{
					job.setMoney(Integer.valueOf(money));
				}
				
				job.setWorkaddress(workaddress);
				
				jobDAO.save(job);
				request.setAttribute("message","添加成功");
				return mapping.findForward("toDetList");
			}
			else
			{
				job = jobDAO.findById(Integer.valueOf(jid));
				jc = jcDAO.findById(Integer.valueOf(jcid));
				job.setJobclass(jc);
				job.setJname(jobName);
				job.setJdes(content);
				job.setAddtime(date);
				job.setNum(Integer.valueOf(num));
				job.setDatelimit(ldate);
				if(money != null &&!money.trim().equals(""))
				{
					job.setMoney(Integer.valueOf(money));
				}
				job.setWorkaddress(workaddress);
				
				jobDAO.update(job);
				
				request.setAttribute("message","更新成功");
				return mapping.findForward("toDetList");
				
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "职位详情", "添加职位详情失败");
			return null;
		}
	}
	
	public ActionForward upJobDes(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String jid = request.getParameter("jid");
		
		
		Job job = new Job();
		JobDAO jobDAO = new JobDAO();
		try
		{
			job = jobDAO.findById(Integer.valueOf(jid));
			request.setAttribute("jobcon",job);
			return mapping.findForward("toDetList");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1","职位详情","获取失败");
		}
		return null;
	}
	
	public ActionForward delJobDes(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response){
		String jid = request.getParameter("jid");
		
		Job job = new Job();
		JobDAO  jobDAO = new JobDAO();
		try{
			job = jobDAO.findById(Integer.valueOf(jid));
			jobDAO.delete(job);
			request.setAttribute("message","删除招聘信息成功");
			return mapping.findForward("toDetList");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1","职位详情","删除失败");
		}
		
		return null;
	}
	

}
